package com.kuanter.kuanterauto.utils;


import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.entity.CacheWeather;
import com.kuanter.kuanterauto.entity.ExpTime;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharePrefenceUtil {
	public static final String SHAREPREFENCE_NAME = "kuanter";
	public static final String DEFAULT_CITY = "city";
	public static final String DEFAULT_CITY_ID = "city_id";
	public static final String LOCATION_LATITUDE = "latitude";
	public static final String LOCATION_LONGTITUDE = "longtitude";
	public static final String MESSAGE_PUSH = "message_push";
	public static final String ROTATESTATUS = "rotate";  //大于0表示地图模式，小于0表示列表模式
	public static final String XINLANG_LOCK = "xinlang_lock";
	public static final String TENGXUN_LOCK = "tengxun_lock";
	public static final String WEIXIN_LOCK = "weixin_lock";
	
	public static final String ALLCITY = "all";
	public static final String CITYCNTER = "cityCenter";
	public static final String CITYIDFORWEATHER = "cityforweather";
	
	public static final String PRIORITY = "priority"; 
	public static final int LIST = -1; // >0的数
	public static final int MAP = 1;  // <0的数
	public static final int NOTCHOSE = 0; //第一次进入没有选择
	
	public static final String USERINFO = "user2Info";
	
	public static final String SORT = "sort";
	
	public static final boolean WK_TIP = true;
	
	public static final String EXP_TIME = "exp_time";
	
	
	public static boolean getXinlangLock(Context context) {
		boolean xinlang_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		xinlang_flag = spf.getBoolean(XINLANG_LOCK, false);
		return xinlang_flag;
	}
	public static void setXinlangLock(Context context,boolean xinlang_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(XINLANG_LOCK,xinlang_flag);
		editor.commit();
	}
	
	public static boolean getTengxunLock(Context context) {
		boolean tengxun_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		tengxun_flag = spf.getBoolean(TENGXUN_LOCK, false);
		return tengxun_flag;
	}
	public static void setTengxunLock(Context context,boolean tengxun_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(TENGXUN_LOCK,tengxun_flag);
		editor.commit();
	}

	public static boolean getWeixinLock(Context context) {
		boolean weixin_flag = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		weixin_flag = spf.getBoolean(WEIXIN_LOCK, false);
		return weixin_flag;
	}
	public static void setWeixinLock(Context context,boolean weixin_flag) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(WEIXIN_LOCK,weixin_flag);
		editor.commit();
	}

	public static boolean getMessagePush(Context context) {
		boolean message_push = true;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		message_push = spf.getBoolean(MESSAGE_PUSH, true);
		return message_push;
	}
	
	public static void setMessagePush(Context context,boolean message_push){
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(MESSAGE_PUSH, message_push);
		editor.commit();
	}
	
	public static String getCity(Context context) {
		String city = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city = spf.getString(DEFAULT_CITY, "");
		return city;
	}
	
	public static void setCity(Context context,String city) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(DEFAULT_CITY, city);
		editor.commit();
	}
	
	/*public static void setCityCenter(Context context,String center) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(CITYCNTER, center);
		editor.commit();
	}
	
	public static String getCityCenter(Context context) {
		String city_center = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_center = spf.getString(CITYCNTER, "");
		return city_center;
	}*/
	
	public static void setCityID(Context context,String id) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(DEFAULT_CITY_ID, id);
		editor.commit();
	}
	
	public static String getCityID(Context context) {
		String city_id = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_id = spf.getString(DEFAULT_CITY_ID, "");
		return city_id;
	}
	
	public static void setCityIDForWeather(Context context,String id) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(CITYIDFORWEATHER, id);
		editor.commit();
	}
	
	public static String getCityIDForWeather(Context context) {
		String city_id = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_id = spf.getString(CITYIDFORWEATHER, "");
		return city_id;
	}
	
	public static void setCenter(Context context,String all) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(ALLCITY, all);
		editor.commit();
	}
	
	public static String getCenter(Context context) {
		String city_center = "";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		city_center = spf.getString(ALLCITY, "");
		return city_center;
	}
	
	public static int getRotateStatus(Context context) {
		int status = NOTCHOSE;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		status = spf.getInt(ROTATESTATUS, MAP);
		return status;
	}
	
	
	public static void setRotateStatus(Context context,int value) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(ROTATESTATUS, value);
		editor.commit();
	}

	public static int getUserID(Context context) {
		int id = 0;
		SharedPreferences sp = context.getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
		String str_id = sp.getString("id", "-1");
		if(!str_id.equals("")) {
			id = Integer.valueOf(str_id);
		}
		return id;
	}
	
	public static boolean checkLogin(Context context) {
		boolean login = false;
		int id = 0;
		SharedPreferences sp = context.getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
		String str_id = sp.getString("id", "");
		if(!str_id.equals("")) {
			id = Integer.valueOf(str_id);
		}
		if(id != 0) {
			login = true;
		}
		return login;
	}
	
	public static GeoPoint getGeopoint(Context context) {
		GeoPoint point = null;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		int latitude = spf.getInt(LOCATION_LATITUDE, 0);
		int longtitude = spf.getInt(LOCATION_LONGTITUDE, 0);
		if(latitude != 0 && longtitude != 0) {
			point = new GeoPoint(latitude, longtitude);
		}
		return point;
	}
	
	public static void setGeopoint(int latitude,int longtitude,Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(LOCATION_LATITUDE, latitude);
		editor.putInt(LOCATION_LONGTITUDE, longtitude);
		editor.commit();
	}
	
	public static void clear(Context context){
		SharedPreferences sp = context.getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}
	
	public static void setChosed(Context context,int priority) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putInt(PRIORITY, priority);
		editor.commit();
	}
	
	public static int getPriority(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		return spf.getInt(PRIORITY, NOTCHOSE);
	}
	
	public static void setSort(Context context,String sort) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(SORT, sort);
		editor.commit();
	}
	
	public static String getSort(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		return spf.getString(SORT, ProApplication.DEFAULT_SORT);
	}
	
	public static void setCacheWeather(Context context,CacheWeather cw) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString("th", cw.getTodayHigh() == null ? "" : cw.getTodayHigh());
		editor.putString("tl", cw.getTodayLow() == null ? "" : cw.getTodayLow());
		editor.putString("ttq", cw.getTodayTQ() == null ? "" : cw.getTodayTQ());
		
		editor.putString("toh", cw.getTomorrowHigh() == null ? "" : cw.getTomorrowHigh());
		editor.putString("tol", cw.getTomorrowLow() == null ? "" : cw.getTomorrowLow());
		editor.putString("totq", cw.getTomorrowTQ() == null ? "" : cw.getTomorrowTQ());
		
		editor.putString("ah", cw.getAfterHigh() == null ? "" : cw.getAfterHigh());
		editor.putString("al", cw.getAfterLow() == null ? "" : cw.getAfterLow());
		editor.putString("atq", cw.getAfterTQ() == null ? "" : cw.getAfterTQ());
		
		editor.commit();
	}
	
	public static CacheWeather getCacheWeather(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		CacheWeather cw = new CacheWeather();
		String th = spf.getString("th", "");
		String tl = spf.getString("tl", "");
		String ttq = spf.getString("ttq", "");
		
		String toh = spf.getString("toh", "");
		String tol = spf.getString("tol", "");
		String totq = spf.getString("totq", "");
		
		String ah = spf.getString("ah", "");
		String al = spf.getString("al", "");
		String atq = spf.getString("atq", "");
		
		cw.setAfterHigh(ah);
		cw.setAfterLow(al);
		cw.setAfterTQ(atq);
		
		cw.setTodayHigh(th);
		cw.setTodayLow(tl);
		cw.setTodayTQ(ttq);
		
		cw.setTomorrowHigh(toh);
		cw.setTomorrowLow(tol);
		cw.setTomorrowTQ(totq);
		return cw;
	}
	
	public static boolean getTip(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		return spf.getBoolean("wk_tip", WK_TIP);
	}
	
	public static void setTip(Context context) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean("wk_tip", false);
		editor.commit();
	}
	
	public static void setExpTime(Context context,ExpTime expTime) {
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putLong(EXP_TIME, expTime.getTime());
		editor.putString("code", expTime.getCode());
		editor.commit();
	}
	
	public static ExpTime getExpTime(Context context) {
		ExpTime expTime = new ExpTime();
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		expTime.setTime(spf.getLong(EXP_TIME, 0l));
		expTime.setCode(spf.getString("code", "00000000"));
		return expTime;
	}
	
	public static void setXXXX(Context context,String xxxx,String date,String sp_name) {
		SharedPreferences spf = context.getSharedPreferences(sp_name, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putString(date, xxxx);
		editor.commit();
	}
	
	public static String getXXXX(Context context,String date,String sp_name) {
		SharedPreferences spf = context.getSharedPreferences(sp_name, Context.MODE_PRIVATE);
		return spf.getString(date, "");
	}
	
	public static String s_load = "w_load";
	public static void setLoad(Context conetxt,boolean load) {
		SharedPreferences spf = conetxt.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		editor.putBoolean(s_load, load);
		editor.commit();
	}
	
	public static boolean getLoad(Context context) {
		boolean hasLoad = false;
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		hasLoad = spf.getBoolean(s_load, hasLoad);
		return hasLoad;
	}
	
	public static void setState(Context conetxt,String state) {
		SharedPreferences spf = conetxt.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = spf.edit();
		if(state != null) {
			editor.putString("state", state);
			editor.commit();
		}
	}
	
	public static String getState(Context context) {
		String state = "0";
		SharedPreferences spf = context.getSharedPreferences(SHAREPREFENCE_NAME, Context.MODE_PRIVATE);
		state = spf.getString("state", state);
		return state;
	}
}
